package com.jstyle.jclifexd.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.jstyle.jclifexd.NetWork.NetWorkUtil;
import com.jstyle.jclifexd.R;
import com.jstyle.jclifexd.model.Activitvtiesmsg;
import com.jstyle.jclifexd.model.Createddata;
import com.jstyle.jclifexd.utils.DateUtil;
import com.jstyle.jclifexd.utils.RxBus;
import com.jstyle.jclifexd.utils.ScreenUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class NotificationAdapter extends RecyclerView.Adapter<BaseViewHolder> {
    List<Createddata> Details;
    Context context;
    //私有属性
    private OnItemClickListener onItemClickListener = null;

    public interface OnItemClickListener {
        void onItemClick(Createddata aa, int position);
    }

    //setter方法
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public NotificationAdapter(Context mycontext) {
        this.context = mycontext;
        this.Details = new ArrayList<>();
    }

    public void SetData(List<Createddata> myDetails) {
        this.Details = myDetails;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_notificatinlist, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(final BaseViewHolder holder, final int position) {
        final Createddata sleepDetail = Details.get(position);
        final ViewHolder viewHolder = (ViewHolder) holder;
        Glide.with(context)
                .load(sleepDetail.getImgUrl())
                .skipMemoryCache(true)// 不使用内存缓存
                .diskCacheStrategy(DiskCacheStrategy.NONE) // 不使用磁盘缓存
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(ScreenUtils.dip2px(context, 11f))))
                .error(context.getResources().getDrawable(R.drawable.hometitle))
                .into(viewHolder.imageview);
        viewHolder.title_name.setText(sleepDetail.getTitle());
        viewHolder.title_time.setText(context.getResources().getString(R.string.times) + DateUtil.gettime(context, sleepDetail.getStart())
                + "-" + DateUtil.gettime(context, sleepDetail.getEnd()));
        viewHolder.title_area.setText(context.getResources().getString(R.string.location) + sleepDetail.getAddress());
        String VALUE = sleepDetail.getType().equals("act") ? context.getResources().getString(R.string.creator) : context.getResources().getString(R.string.create_team);
        viewHolder.title_user.setText(VALUE + sleepDetail.getCreaterName());
        viewHolder.setting_count.setText((sleepDetail.getNumber() + "").equals("9999") ? context.getResources().getString(R.string.unlimited) : sleepDetail.getNumber() + "");
        viewHolder.getting_count.setText(sleepDetail.getJoinNumber() + "");
        viewHolder.applicant.setText(context.getResources().getString(R.string.applicant) + sleepDetail.getNickname());

        viewHolder.title_stustusCom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Querydata(sleepDetail, true);
            }
        });
        viewHolder.cancl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Querydata(sleepDetail, false);
            }
        });


    }


    private void Querydata(Createddata createddata, boolean vaule) {
        Map<String, Object> map = new HashMap<>();
        map.put("uid", createddata.getApplyUid()+"");//"uid" -> "111000036"
        map.put("actId", createddata.getActId()+"");
        map.put("result", vaule ? "1" : "3");
        NetWorkUtil.getInstance().getJstyleApi().saveAuditResult(map).
                subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Activitvtiesmsg>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(Activitvtiesmsg activitvtiesData) {
                if (activitvtiesData.getMsgCode() == 1) {
                    RxBus.getInstance().post("reload_NotificationlistActivity");
                    Toast.makeText(context, context.getResources().getString(R.string.submit_successfully), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onComplete() {
            }
        });

    }

    @Override
    public int getItemCount() {
        return null == Details ? 0 : Details.size();
    }


    static class ViewHolder extends BaseViewHolder {
        @BindView(R.id.img_cearted)
        ImageView imageview;
        @BindView(R.id.title_name)
        TextView title_name;
        @BindView(R.id.title_time)
        TextView title_time;
        @BindView(R.id.title_area)
        TextView title_area;
        @BindView(R.id.title_user)
        TextView title_user;
        @BindView(R.id.setting_count)
        TextView setting_count;
        @BindView(R.id.getting_count)
        TextView getting_count;
        @BindView(R.id.applicant)
        TextView applicant;

        @BindView(R.id.title_stustusCom)
        Button title_stustusCom;
        @BindView(R.id.cancl_)
        Button cancl;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

}
